<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="学号" prop="xh">
        <el-input
          v-model="queryParams.xh"
          placeholder="请输入学号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入学年度" prop="rxNd">
        <el-input
          v-model="queryParams.rxNd"
          placeholder="请输入入学年度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班级" prop="bj">
        <el-input
          v-model="queryParams.bj"
          placeholder="请输入班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专业" prop="zy">
        <el-select v-model="queryParams.zy" placeholder="请选择专业" clearable>
          <el-option
            v-for="dict in dict.type.zy"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学制" prop="xz">
        <el-select v-model="queryParams.xz" placeholder="请选择学制" clearable>
          <el-option
            v-for="dict in dict.type.xz"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学籍状态" prop="xjZt">
        <el-select v-model="queryParams.xjZt" placeholder="请选择学籍状态" clearable>
          <el-option
            v-for="dict in dict.type.xj_zt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['xsxx:xjxx:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['xsxx:xjxx:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['xsxx:xjxx:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['xsxx:xjxx:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="xjxxList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->

      <el-table-column label="学生信息Id" align="center" prop="jbxxId" />
<!--      <el-table-column label="学生姓名" align="center" prop="xm" />-->
      <el-table-column label="学号" align="center" prop="xh" />
      <el-table-column label="入学年度" align="center" prop="rxNd" />
      <el-table-column label="班级" align="center" prop="bj" />
      <el-table-column label="专业" align="center" prop="zy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.zy" :value="scope.row.zy"/>
        </template>
      </el-table-column>
      <el-table-column label="学制" align="center" prop="xz">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xz" :value="scope.row.xz"/>
        </template>
      </el-table-column>
      <el-table-column label="学籍状态" align="center" prop="xjZt">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xj_zt" :value="scope.row.xjZt"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['xsxx:xjxx:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['xsxx:xjxx:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学籍信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-card class="box-card">
          <div slot="header" class="clearfix" style="text-align: center">
            <span style="font-size: 20px;color: #1890ff">学籍基本信息</span>
          </div>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="学生姓名" prop="xm">
                <el-input
                  v-model="form.xm"
                  placeholder="请选择学生"
                  readonly
                  @click.native="showStudentDialog = true"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="学号" prop="xh">
                <el-input v-model="form.xh" placeholder="请输入学号"  />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row :gutter="24">

            <el-col :span="12">
              <el-form-item label="入学年度" prop="rxNd">
                <el-input v-model="form.rxNd" placeholder="请输入入学年度" />
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="班级" prop="bj">
                <el-input v-model="form.bj" placeholder="请输入班级" />
              </el-form-item>
            </el-col>

          </el-row>


          <el-row :gutter="24">

            <el-col :span="12">
              <el-form-item label="专业" prop="zy">
                <el-select v-model="form.zy" placeholder="请选择专业">
                  <el-option
                    v-for="dict in dict.type.zy"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="学制" prop="xz">
                <el-select v-model="form.xz" placeholder="请选择学制">
                  <el-option
                    v-for="dict in dict.type.xz"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>
          <el-form-item label="学籍状态" prop="xjZt">
            <el-select v-model="form.xjZt" placeholder="请选择学籍状态">
              <el-option
                v-for="dict in dict.type.xj_zt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-card>
        <br>
        <el-card class="box-card">
          <div slot="header" class="clearfix" style="text-align: center">
            <span style="font-size: 20px;color: #1890ff">学籍附件信息</span>
          </div>
          <!--        <el-divider content-position="center">学籍附件信息</el-divider>-->
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddXsxxXjxxFj">添加</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteXsxxXjxxFj">删除</el-button>
            </el-col>
          </el-row>
          <el-table :data="xsxxXjxxFjList" :row-class-name="rowXsxxXjxxFjIndex" @selection-change="handleXsxxXjxxFjSelectionChange" ref="xsxxXjxxFj">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" align="center" prop="index" width="50"/>
            <el-table-column label="附件大小" prop="fileSize"/>
            <el-table-column label="上传人" prop="createBy"/>
            <el-table-column label="上传时间" prop="createTime"/>
          </el-table>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <!-- 嵌入学生选择弹窗 -->
      <XsXxDx :visible.sync="showStudentDialog" @select="handleStudentSelect" />
    </el-dialog>
  </div>
</template>

<script>
import { listXjxx, getXjxx, delXjxx, addXjxx, updateXjxx } from "@/api/xsxx/xjxx"
import XsXxDx from "@/views/components/xsxxxz/XsXxDx.vue";

export default {
  name: "Xjxx",
  components: {XsXxDx},
  dicts: ['zy', 'xj_zt', 'xz'],
  provide(){
    return{
      updateXsXxLable:(val)=>{
        console.log(val,'从组件传回来的数据'),
          this.form.jbxxId=val.jbxxId
      }
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedXsxxXjxxFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学籍信息表格数据
      xjxxList: [],
      // 学籍附件表格数据
      xsxxXjxxFjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      showStudentDialog: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jbxxId: null,
        xh: null,
        rxNd: null,
        bj: null,
        zy: null,
        xz: null,
        xjZt: null,
        userId: null,
        deptId: null,
        xm:null,
      },
      // 表单参数
      form: {
        xjxxId: null,
        jbxxId: null,
        xm: null,
        xh: null,
        rxNd: null,
        bj: null,
        zy: null,
        xz: null,
        xjZt: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      },
      // 表单校验
      rules: {
        // 新加的
        xm: [
          { required: true, message: "请选择学生", trigger: "change" }
        ],





        // 以前的
        // jbxxId: [
        //   { required: true, message: "请选择学生", trigger: "change" }
        // ],
        xh: [
          { required: true, message: "学号不能为空", trigger: "blur" }
        ],
        rxNd: [
          { required: true, message: "入学年度不能为空", trigger: "blur" }
        ],
        bj: [
          { required: true, message: "班级不能为空", trigger: "blur" }
        ],
        zy: [
          { required: true, message: "专业不能为空", trigger: "change" }
        ],
        xz: [
          { required: true, message: "学制不能为空", trigger: "change" }
        ],
        xjZt: [
          { required: true, message: "学籍状态不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleStudentSelect(student) {
      if (student) {
        this.form.jbxxId = student.jbxxId
        this.form.xm = student.xm
        this.form.xh = student.xh
        this.showStudentDialog = false
      }
    },
    /** 查询学籍信息列表 */
    getList() {
      this.loading = true
      listXjxx(this.queryParams).then(response => {
        this.xjxxList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        xjxxId: null,
        jbxxId: null,
        xm: null,
        xh: null,
        rxNd: null,
        bj: null,
        zy: null,
        xz: null,
        xjZt: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null,

      }
      this.xsxxXjxxFjList = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.xjxxId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加学籍信息"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const xjxxId = row.xjxxId || this.ids
      getXjxx(xjxxId).then(response => {
        this.form = response.data
        this.xsxxXjxxFjList = response.data.xsxxXjxxFjList
        this.open = true
        this.title = "修改学籍信息"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.xsxxXjxxFjList = this.xsxxXjxxFjList
          if (this.form.xjxxId != null) {
            updateXjxx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addXjxx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const xjxxIds = row.xjxxId || this.ids
      this.$modal.confirm('是否确认删除学籍信息编号为"' + xjxxIds + '"的数据项？').then(function() {
        return delXjxx(xjxxIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 学籍附件序号 */
    rowXsxxXjxxFjIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 学籍附件添加按钮操作 */
    handleAddXsxxXjxxFj() {
      let obj = {}
      obj.fileName = ""
      obj.fileSize = ""
      obj.createBy = ""
      obj.createTime = ""
      this.xsxxXjxxFjList.push(obj)
    },
    /** 学籍附件删除按钮操作 */
    handleDeleteXsxxXjxxFj() {
      if (this.checkedXsxxXjxxFj.length == 0) {
        this.$modal.msgError("请先选择要删除的学籍附件数据")
      } else {
        const xsxxXjxxFjList = this.xsxxXjxxFjList
        const checkedXsxxXjxxFj = this.checkedXsxxXjxxFj
        this.xsxxXjxxFjList = xsxxXjxxFjList.filter(function(item) {
          return checkedXsxxXjxxFj.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleXsxxXjxxFjSelectionChange(selection) {
      this.checkedXsxxXjxxFj = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('xsxx/xjxx/export', {
        ...this.queryParams
      }, `xjxx_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>


<style scoped>
.el-input.is-readonly .el-input__inner {
  background-color: #f5f7fa;
  cursor: pointer;
}
</style>
